package com.qf;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.ProjectDao;
import com.qf.dao.SubitemDao;
import com.qf.pojo.Project;
import com.qf.pojo.Subitem;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * 公众号：Java架构栈
 * @Author: 卓不凡
 */
public class Demo {
    private  InputStream resourceAsStream;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
    private  ProjectDao projectDao;
    private SubitemDao subitemDao;
    @Before
    public  void init() throws IOException {
        //mybatis的核心是sqlsession  sqlsessionfactory去获取
         resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //构建对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //工厂对象
         sqlSessionFactory = builder.build(resourceAsStream);
        //sqlsession
       sqlSession = sqlSessionFactory.openSession(true);

    }
    @After
    public void  close() throws Exception{
        //释放资源
        sqlSession.close();
        resourceAsStream.close();

    }
    @Test
    public void demo01() throws Exception{

        //代理dao
       projectDao = sqlSession.getMapper(ProjectDao.class);

        PageHelper.startPage(1,5);
        //执行
        List<Project> projects = projectDao.findProjects();
        PageInfo<Project> projectPageInfo=new PageInfo<>(projects);
        System.out.println("当前页："+projectPageInfo.getPageNum());
        System.out.println("每页数量："+projectPageInfo.getPageSize());
        System.out.println("当前页数量："+projectPageInfo.getSize());
        System.out.println("当前页数据："+projectPageInfo.getList());
        //遍历
//        projects.stream().forEach(o-> System.out.println(o));
//        for (Project project : projects) {
//            System.out.println(project);
//        }


    }
    @Test
    public void demo02() throws Exception{

        //代理dao
        subitemDao = sqlSession.getMapper(SubitemDao.class);

        //执行
        List<Subitem> subitems = subitemDao.findSubitems();
        //遍历
        subitems.stream().forEach(o-> System.out.println(o));


    }
    @Test
    public void demo03() throws Exception{

        //代理dao
        projectDao = sqlSession.getMapper(ProjectDao.class);
        Project projectByid=projectDao.findProjectByPid(6);
        System.out.println(projectByid);
        //执行


    }
    @Test
    public void demo04() throws Exception{
        projectDao = sqlSession.getMapper(ProjectDao.class);
        Project project = new Project();
        project.setPname("西安大雁塔");
        project.setStarttime(new Date());
        project.setEndtime(new Date());
        projectDao.addProject(project);

    }
    @Test
    public void TestFindSubitems() throws IOException {
        subitemDao = sqlSession.getMapper(SubitemDao.class);
        List<Subitem> subitems = subitemDao.findSubitems();
        for(Subitem subitem:subitems){
            System.out.println(subitem);
        }
    }
    @Test
    public void TestAddSubitem(){
        subitemDao = sqlSession.getMapper(SubitemDao.class);
        Subitem subitem = new Subitem();
        subitem.setSuid("16");
        subitem.setSname("大工程");
        subitem.setStarttime(new Date());
        subitem.setEndtime(new Date());
        int i = this.subitemDao.addSubitem(subitem);
        System.out.println(i);
    }
}
